package cn.edu.zafu.bigdata.x.wupeng.h;

import java.io.*;

public class DataIOTest {
    public static void main(String[] args) throws IOException {
        //定义要保存的数据数组。
        double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
        int[] units = { 12, 8, 13, 29, 50 };
        String[] descs = { "Java T-shirt", "Java Mug",
                            "Duke Juggling Dolls",
                            "Java Pin", "Java Key Chain" };


        FileOutputStream fout = new FileOutputStream("files/invoice.txt");
       try(DataOutputStream out = new DataOutputStream(fout)) {
           try {
               for (int i = 0; i < prices.length; i++) {
                   out.writeDouble(prices[i]);
                   out.writeChar('\t');
                   out.writeInt(units[i]);
                   out.writeChar('\t');
                   out.writeUTF(descs[i]);
                   out.writeChar('\t');
               }

           } catch (IOException e) {
               throw new RuntimeException(e);
           }
       }

        FileInputStream fin = new FileInputStream("files/invoice.txt");
        DataInputStream in = new DataInputStream(fin);
        try {
            for(int i=0;i<prices.length;i++){
                double price = in.readDouble();
                in.readChar();
                int unit = in.readInt();
                in.readChar();
                String desc = in.readUTF();
                in.readChar();
                double total = price * unit;
                System.out.println("Item: " + desc + " " + unit + " units at $" + price + " each. Total is $" + total);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        in.close();

    }
}
